package com.vmove.childtrain.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vmove.childtrain.R;

import java.lang.ref.WeakReference;


public class MyCheckBox111 extends LinearLayout implements OnClickListener{
	private int mSpacing;
	private int mBackgroundRes;
	private int mTextSize;
	private int mTextColor;
	private String mText;
	private String mLabelText;
	private boolean mChecked;

	private TextView mCheckBox;
	private TextView mTextView;

	private WeakReference<OnMyCheckBoxClickListener> mListener;

	public MyCheckBox111(Context context) {
		super(context);
	}

	public MyCheckBox111(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.mycheckbox);
		mChecked = a.getBoolean(R.styleable.mycheckbox_checked, false);
		mTextSize = a.getInt(R.styleable.mycheckbox_ctext_size, 14);
		mTextColor = a.getColor(R.styleable.mycheckbox_ctext_color, 0XFF333333); 
		mText = a.getString(R.styleable.mycheckbox_ctext);
		mLabelText = a.getString(R.styleable.mycheckbox_labeltext);
		mBackgroundRes = a.getResourceId(R.styleable.mycheckbox_src, -1);
		mSpacing = a.getInt(R.styleable.mycheckbox_spacing, 24);
		a.recycle();
		
		LayoutInflater.from(context).inflate(R.layout.widget_my_checkbox, this, true);
		mCheckBox = (TextView)this.findViewById(R.id.my_checkbox);
		mCheckBox.setBackgroundResource(mBackgroundRes);
		mCheckBox.setSelected(mChecked);
		mCheckBox.setText(mLabelText);
		mTextView = this.findViewById(R.id.my_checkbox_text);
		if(null != mText){
			mTextView.setText(mText);
//	        LayoutParams lp = new LayoutParams(
//	        		android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
//	        		android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
//	        lp.setMargins(mSpacing, 0, 0, 0);
//	        mTextView.setLayoutParams(lp);
//	        mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP ,mTextSize);
//	        mTextView.setTextColor(mTextColor);
	        mTextView.setVisibility(View.VISIBLE);
		}
		View v = this.findViewById(R.id.my_checkbox_linearLayout);
		v.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		mCheckBox.setSelected(!mChecked);
		mTextView.setSelected(!mChecked);
		mChecked = !mChecked;
		if(null != mListener && null != mListener.get()){
			mListener.get().onMyCheckBoxClick(MyCheckBox111.this);
		}
	}
	
	public void setOnMyCheckBoxClickListener(OnMyCheckBoxClickListener listener) {
		mListener = new WeakReference<OnMyCheckBoxClickListener>(listener);
	}
	
	public interface OnMyCheckBoxClickListener {
        abstract void onMyCheckBoxClick(View v);
    }

	public void setChecked(boolean isAllCheck) {
		mCheckBox.setSelected(isAllCheck);
	}

	public boolean isChecked() {
		mChecked = mCheckBox.isSelected();
		return mChecked;
	}

	public void setText(String text){
		mTextView.setText(text);
	}

	public String getmText() {
		return mText;
	}
	
}
